<?php
namespace App\Models\Scope;

use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use DefaultCompany;

/**
 * 公司限制
 * @author litc
 */
class CompanyScope implements Scope {
    
    
    public function apply(Builder $builder, Model $model)
    {
        $table = $model->getTable();
        $user = auth()->user();
        if(!$user) {
            return null;
        }
        if($user->isSuper()) {
            return $builder;
        }

        $cid = $user->Co_ID;
        $builder -> where($table.'.Co_ID', '=', $cid);
        return $builder;
    }
}
